import numpy as np
import matplotlib.pyplot as plt
def model(x,theta):
    return x.dot(theta)

def cost(h,y):
    return 0.5*np.mean((h-y)**2)

def grad(x,y,iter0=5000,alpha=0.001):
    m,n=x.shape
    theta=np.zeros(n)
    J=np.zeros(iter0)
    for i in range(iter0):
        h=model(x,theta)
        J[i]=cost(h,y)
        dt=1/m*x.T.dot(h-y)
        theta-=alpha*dt
    return h,theta,J

if __name__ == '__main__':
    train=np.loadtxt('train.txt',delimiter=',')
    test=np.loadtxt('test.txt',delimiter=',')
    train_x=train[:,:-1]
    train_y=train[:,-1]
    test_x=test[:,:-1]
    test_y=test[:,-1]

    train_X=np.c_[np.ones(len(train_x)),train_x]
    test_X=np.c_[np.ones(len(test_x)),test_x]

    train_h, theta, J=grad(train_x,train_y)
    plt.plot(J)
    plt.show()

    test_h=model(test_x,theta)

    plt.scatter(test_x,test_y)
    plt.scatter(test_x,test_h)
    plt.show()
